#pragma once
#include <ctemplate/template.h>
#include <iostream>
#include <string>
#include "oj_model.hpp"

namespace ns_view
{
    using namespace ns_model;
    using namespace std;
    class View
    {
    public:
        View(){}
        ~View(){}

        const std::string template_path = "./template_html/";
        void ExpandAllHtml(const vector<Question>& question_list,std::string *html)
        {
            //形成路径
            std::string src = template_path + "all_questions.html";
            //构建数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for(auto& q:question_list)
            {
                ctemplate::TemplateDictionary *sub=root.AddSectionDictionary("question_list");
                sub->SetValue("name",q.name);
                sub->SetValue("title",q.title);
                sub->SetValue("star",q.star);
            }
            //获取被渲染网页
            ctemplate::Template *tpl=ctemplate::Template::GetTemplate(src,ctemplate::DO_NOT_STRIP);
            //完成渲染
            tpl->Expand(html,&root);
        }

        void ExpandOneHtml(const Question& question,std::string *html)
        {
            //构建路径
            std::string src = template_path + "one_question.html";
            //构建数据字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("name",question.name);
            root.SetValue("title",question.title);
            root.SetValue("star",question.star);
            root.SetValue("desc",question.desc);
            root.SetValue("pre_code",question.hander);
            //获取被渲染网页
            ctemplate::Template *tpl=ctemplate::Template::GetTemplate(src,ctemplate::DO_NOT_STRIP);
            //完成渲染
            tpl->Expand(html,&root);
        }
    };   
}
